﻿using InteControlSys.SampleService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;

namespace InteControlSys.ViewModel
{
    public class SampleAlarmViewModel : NodeViewModel
    {
        public SampleService.SampleClient sp = new SampleClient("WSHttpBinding_ISample");
        public SampleCommon sc = new SampleCommon();

        public SampleAlarmViewModel(string name)
        {
            this.Name = name;
            this.Parent = null;
            this.IsExpanded = true;
            this.NodeType = "SampleAlarmViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();

            dpBegin = DateTime.Now.AddDays(-7);
            dpEnd = DateTime.Now;
            Init();
        }

        public SampleAlarmViewModel()
        {
            dpBegin = DateTime.Now.AddDays(-7);
            dpEnd = DateTime.Now;
            Init();
        }
        public override Uri GetMyIcon()
        {
            return new Uri("../Resources/Images/bucket.png", UriKind.RelativeOrAbsolute);
        }

      

        #region properties
        private DateTime _dpBegin;

        public DateTime dpBegin
        {
            get { return _dpBegin; }
            set
            {
                _dpBegin = value;
                NotifyChanged("dpBegin");
            }
        }

        private DateTime _dpEnd;

        public DateTime dpEnd
        {
            get { return _dpEnd; }
            set
            {
                _dpEnd = value;
                NotifyChanged("dpEnd");
            }
        }

        private int _selectedStatusIndex;
        public int SelectedStatusIndex
        {
            get { return _selectedStatusIndex; }
            set
            {
                _selectedStatusIndex = value;
                NotifyChanged("SelectedStatusIndex");
             
            }
        }

        private string _errorMSG = null;
        public string ErrorMSG
        {
            get { return _errorMSG; }
            set
            {
                _errorMSG = value;
                NotifyChanged("ErrorMSG");
            }
        }

        private string _selectAllName;

        public string SelectAllName
        {
            get { return _selectAllName; }

            set
            {
                _selectAllName = value;
                NotifyChanged("SelectAllName");
            }
        }

        private bool _selectAll;

        public bool SelectAll
        {
            get { return _selectAll; }

            set
            {
                _selectAll = value;
                NotifyChanged("SelectAll");
                // MakeSelectAll();
            }
        }

        private ObservableCollection<SampleAlarmList> sampleAlarmList;
        public ObservableCollection<SampleAlarmList> SampleAlarmList
        {
            get { return sampleAlarmList; }
            set
            {
                sampleAlarmList = value;
                NotifyChanged("SampleAlarmList");
            }
        }


        #endregion

        #region Command
        public void Init()
        {
            GetList();
            _selectAll = false;
            _selectAllName = "全选";
        }

        public void GetList()
        {
            var agb = sp.GetAnaGroupAlarm(dpBegin, dpEnd, SelectedStatusIndex);
            sampleAlarmList = new ObservableCollection<SampleAlarmList>();
            foreach (var item in agb)
            {
                var bi = new SampleAlarmList();
               
                bi.IsSelected = false;
                bi.GID = item.ID;
                bi.AnalysBatchID = item.质检凭证号;
                bi.Materiel = item.物料.Name;
                bi.Status = sc.DicSampleStatus[item.检验状态];
                bi.Modifier = item.修改人 != null ? item.修改人.CName : string.Empty;
                bi.ModifierID = item.修改人 != null ? item.修改人.ID : Guid.Empty;
                bi.ModifyTime = item.修改时间;

                var bucket = sp.GetBucketByGroupID(item.ID);
                if (bucket != null)
                {
                    bi.BucketID1 = bucket[0].样桶号;
                    bi.Weight1 = bucket[0].大样重量;
                    if (bucket.Count() > 1)
                    {
                        bi.BucketID2 = bucket[1].样桶号;
                        bi.Weight2 = bucket[1].大样重量;
                    }
                    bi.TotalWeight = bi.Weight1 + (bi.Weight2 ?? 0);
                }

                sampleAlarmList.Add(bi);
            }
            this.SampleAlarmList = sampleAlarmList;
        }
        public void MakeSelectAll()
        {
            if (sampleAlarmList != null && _selectAll == false)
            {
                foreach (var item in sampleAlarmList)
                {
                    item.IsSelected = true;
                }
                _selectAll = true;
                _selectAllName = "全不选";
            }
            else if (sampleAlarmList != null && _selectAll == true)
            {
                foreach (var item in sampleAlarmList)
                {
                    item.IsSelected = false;
                }
                _selectAll = false;
                _selectAllName = "全选";
            }
            this.SampleAlarmList = sampleAlarmList;
            SelectAll = _selectAll;
            SelectAllName = _selectAllName;
        }

        public ICommand SelectOrUnSelectAll
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    MakeSelectAll();
                });
            }
        }

        public ICommand Query
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    GetList();
                });
            }
        }

        public ICommand ContinueSample
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    sampleAlarmList = this.SampleAlarmList;
                    foreach (var item in sampleAlarmList)
                    {
                        if (item.IsSelected)
                        {
                            if (item.Status != "重量报警")
                            {
                                this.ErrorMSG = "只有重量报警的试样可下达继续制样指令!";
                                break;
                            }
                            else
                            {
                                //调用发送封桶指令的接口

                                if (sp.SetAnaGroupStatus(item.GID,3)) i++;
                            }
                        }
                    }
                    GetList();
                    if (i > 0) this.ErrorMSG = "已下达" + i.ToString() + "条继续制样指令！";
                });
            }
        }

        public ICommand DropSample
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    sampleAlarmList = this.SampleAlarmList;
                    foreach (var item in sampleAlarmList)
                    {
                        if (item.IsSelected)
                        {
                            if (item.Status != "重量报警")
                            {
                                this.ErrorMSG = "只有重量报警的试样可下达判定弃样指令!";
                                break;
                            }
                            else
                            {
                                //调用发送封桶指令的接口

                                if (sp.SetAnaGroupStatus(item.GID, 4)) i++;
                            }
                        }
                    }
                    GetList();
                    if (i > 0) this.ErrorMSG = "已下达" + i.ToString() + "条判定弃样指令！";
                });
            }
        }

        public ICommand BackContinue
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    sampleAlarmList = this.SampleAlarmList;
                    foreach (var item in sampleAlarmList)
                    {
                        if (item.IsSelected)
                        {
                            if (item.Status != "继续制样")
                            {
                                this.ErrorMSG = "只有继续制样的试样可下达取消继续制样指令!";
                                break;
                            }
                            else
                            {
                                //调用发送封桶指令的接口

                                if (sp.SetAnaGroupStatus(item.GID, 2)) i++;
                            }
                        }
                    }
                    GetList();
                    if (i > 0) this.ErrorMSG = "已下达" + i.ToString() + "条取消继续制样指令！";
                });
            }
        }

        public ICommand BackDrop
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    int i = 0;
                    this.ErrorMSG = null;
                    sampleAlarmList = this.SampleAlarmList;
                    foreach (var item in sampleAlarmList)
                    {
                        if (item.IsSelected)
                        {
                            if (item.Status != "判定弃样")
                            {
                                this.ErrorMSG = "只有判定弃样的试样可下达取消弃样指令!";
                                break;
                            }
                            else
                            {
                                //调用发送封桶指令的接口

                                if (sp.SetAnaGroupStatus(item.GID, 2)) i++;
                            }
                        }
                    }
                    GetList();
                    if (i > 0) this.ErrorMSG = "已下达" + i.ToString() + "条取消继弃样指令！";
                });
            }
        }
        #endregion
    }

    public class SampleAlarmList : INPCBase
    {
        public SampleAlarmList() { }

        //public bool IsSelected { get; set; }
        private bool _isSelected = true;

        public bool IsSelected
        {
            get { return _isSelected; }

            set
            {
                _isSelected = value;
                NotifyChanged("IsSelected");
            }
        }

        public Guid GID { get; set; }

        public string AnalysBatchID { get; set; }
        public string Materiel { get; set; }

        public string Status { get; set; }

        public string BucketID1 { get; set; }

        public double? Weight1 { get; set; }

        public string BucketID2 { get; set; }

        public double? Weight2 { get; set; }

        public double? TotalWeight { get; set; }

        public string Modifier { get; set; }
        public Guid? ModifierID { get; set; }

        public DateTime? ModifyTime { get; set; }

    }
}
